package leetCode.solution;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
    	if(head==null || (head.next==null && n==1)) return null;
    	ListNode l = head;
    	ListNode r = head;
    	for(int i=0;i<n;i++) {
    		r = r.next;
    	}
    	while(r.next!=null) {
    		l=l.next;
    		r=r.next;
    	}
    	l.next = l.next.next;
    	if(l==head) {
    		return head.next;
    	}
    	return head;
    }
}